//
//  smartImage.m
//  Test01
//
//  Created by Storm Christoph on 10/30/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Card.h"

#define kTransitionDuration	0.4

@implementation Card 
@synthesize delegate;		// creats Get and Set's
@synthesize cardID;
@synthesize groupID;
@synthesize isFlipped;

-(id)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
		frontImageView = nil;
		backImageView = nil;
	}
	return self;
}

-(id)initWithFrame:(CGRect)frame frontImage:(UIImage*)frontImg  backImage:(UIImage *)backImg gameCardID:(int)cID gameGroupID:(int)gID
{
	if (self = [super initWithFrame:frame]) {
		startLocation = CGPointMake(0, 0);
		CGRect imgFrame;
		imgFrame.origin.x=0;
		imgFrame.origin.y=0;
		imgFrame.size = frame.size;
		
		cardID = cID;
		groupID = gID;
		isFlipped = FALSE;
		
		frontImageView = [[UIImageView alloc] initWithFrame:imgFrame];
		backImageView = [[UIImageView alloc] initWithFrame:imgFrame];
		frontImageView.contentMode = UIViewContentModeScaleAspectFill;
		backImageView.contentMode = UIViewContentModeScaleAspectFill;
		frontImageView.image = frontImg;
		self.clipsToBounds = YES;
		backImageView.image = backImg;		
		[self addSubview:frontImageView];
	}
	
	return self;
}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
	// Retrieve the touch point 
	CGPoint pt = [[touches anyObject] locationInView:self]; 
	startLocation = pt; 
	[[self superview] bringSubviewToFront:self]; 
} 

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 
	// Move relative to the original touch point 
	CGPoint pt = [[touches anyObject] locationInView:self]; 
	CGPoint center = [self center]; 
	center.x += pt.x - startLocation.x; 
	center.y += pt.y - startLocation.y; 
	[self setCenter:center]; 
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	
	[self askController];
}


- (void)testFunkt:(int)test{

	NSLog(@"TEST: %i",test);
}

- (IBAction)notifyController
{
	if ([delegate respondsToSelector:@selector(cardFlipped:)])
	{
		[delegate cardFlipped:self];
	}
}

- (IBAction)askController; 
{ 	
	BOOL flip = NO;
	
	if ([delegate respondsToSelector:@selector(cardShouldFlip:)]) {
		flip = [delegate cardShouldFlip:self];
	}
	
	if(flip)
	{
		[self flipAction:self];
		
	}
	
	
	//NSLog(@"flip: %i", flip);
}

-(void)doFlip {

	if (isFlipped) {
		[self flipAction:self];
		isFlipped = NO;
	}
	
}

// notification not used
/*
-(void)handleNotification:(NSNotification*)note{
	//[self flipAction:self];
}
*/

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
	[self notifyController];
}

- (IBAction)flipAction:(id)sender
{
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:kTransitionDuration];
	[UIView setAnimationTransition:([frontImageView superview] != nil ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight) forView:self cache:YES];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
	if ([frontImageView superview] != nil)
	{
		[frontImageView removeFromSuperview];
		[self addSubview:backImageView];
	}
	else
	{
		[backImageView removeFromSuperview];
		[self addSubview:frontImageView];
	}
	[UIView commitAnimations];
}


-(void)dealloc {
	[frontImageView release];
	[backImageView release];
	[super dealloc];
}

@end
